'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:FinanceFBzjCtrl
 * @description
 * # FinanceFBzjCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('FinanceFBzjCtrl', function($scope, $rootScope, openApi, Util, $location, $routeParams, FormSubmit) {
        var id = $scope.process_id = $routeParams.id
        $scope.payTypeList = [{
            key: "汇投标保证金",
            value: 1
        },{
            key: "退投标保证金",
            value: 3
        }];
        $scope.payWayList = [{
            key: "银行保函",
            value: 1
        }, {
            key: "柜台转账",
            value: 2
        }, {
            key: "网银转账",
            value: 3
        }, {
            key: "银行汇票",
            value: 4
        }, {
            key: "现金支付",
            value: 5
        }, {
            key: "担保保函",
            value: 6
        }, {
            key: "保险保函",
            value: 7
        },  {
            key: "其他",
            value: 8
        }, {
            key: "银行存单",
            value: 9
        }];
        $scope.lastReturnBackType = [
            {
              key: "未退清",
              value: 0
            },
            {
              key: "已退清",
              value: 1
            }
          ];
        $scope.resetform = function() {
            $scope.param.depositReturnId = "";
            $scope.param.fundSorucePerson = "";
            $scope.param.fundSoruceAccount = "";
            $scope.param.fundSoruceBank = "";
            $scope.param.fundSoruceMoney = "";
            $scope.param.receiveMoney = "";
            $scope.param.receivePerson = "";
            $scope.param.receiveAccount = "";
            $scope.param.receiveBank = "";
            $scope.param.fundSoruceList = [];
            $scope.param.receiveList = [];
            if(typeof $scope.param.lastReturnBack==="undefined"){
                $scope.param.lastReturnBack=$scope.param.lastReturnBack||1
            }
        }
        $scope.choosePeople = function() {
            if ($scope.param.payType == 3||$scope.param.payType == 4) {
                return
            }
            var relModuleObj = {
                name: "FinanceFBzjCtrl",
                selectTab: "deposit_select_project",
                selectTabContent: "工程名称"
            };
            // 将列表的数据序列化存入缓存以便回来时候使用
            $scope.param.fundSoruceDetail = JSON.stringify($scope.param.fundSoruceList)
            $scope.param.receiveDetail = JSON.stringify($scope.param.receiveList)
            Util.savaParamForReview($scope.param, id);
            Util.savaDataforsearch(relModuleObj);

            $location.path("finance/choosepeople")
        };
        $scope.disabled = function() {
            var checked = true;
            if (!$scope.param) {
                return
            }
            if ($scope.param.payType == 3||$scope.param.payType == 4) {
                checked = false;
            }
            return checked
        };
        $scope.chooseNo = function(ifchoose) {
            // 如果选择了退投标保证金则选择跳入页面选择编号
            if ($scope.disabled()) {
                return;
            }
            var relModuleObj = {
                name: "FinanceFBzjCtrl",
                selectTab: "deposit_select_deposit",
                selectTabContent: "保证金申请单"
            };
            // 将列表的数据序列化存入缓存以便回来时候使用
            $scope.param.fundSoruceDetail = JSON.stringify($scope.param.fundSoruceList)
            $scope.param.receiveDetail = JSON.stringify($scope.param.receiveList)
            Util.savaParamForReview($scope.param, id);
            Util.savaDataforsearch(relModuleObj);

            $location.path("finance/choosepeople")
        };
        $scope.addfundSorucePenson = function() {
            $rootScope.$emit('showmasker');
            $scope.showFundPerson = true;
        };
        $scope.deletedItem = function(index) {
            $scope.param.fundSoruceList.splice(index, 1);
            $scope.index = -1;
        };
        $scope.confirmFundPerson = function() {
            $scope.picker = $scope.picker || {};
            var check = false;
            check = checkPicker($scope.picker);
            if (!check) {
                return;
            };
            $scope.param.fundSoruceList.push($scope.picker);
            // 隐藏表单
            hideFundPerson();
        };
        var hideFundPerson = function() {
            $scope.picker = {};
            $rootScope.$emit('closemasker');
            $scope.showFundPerson = false;
        };
        $scope.$on('closemasker', function() {
            hideFundPerson()
        });
        $scope.cacelFundPerson = function() {
            hideFundPerson();
        };
        $scope.swipe = function(index) {
            $scope.index = index;
        }
        $scope.resetlist = function(index) {
                $scope.index = -1;
            }
            // 验证是否输入完整
        var checkPicker = function(data) {
            var check = true;
            if (!data.money) {
                Util.Toast("请输入付款金额");
                check = false;
                return check;
            }
            return check;

        };
        $scope.submit = function(operationId) {

            if ($scope.param.payType == 1||$scope.param.payType == 2) {
                $scope.param.fundSoruceDetail = angular.toJson($scope.param.fundSoruceList)
            } else if ($scope.param.payType == 3||$scope.param.payType == 4) {
                $scope.param.receiveDetail = angular.toJson($scope.param.receiveList)
            }
            // 底部类型end
            var options = {};
            // 流程id
            options.id = id;
            // 根据operationId判断是草稿还是提交
            options.operationId = operationId;
            options.urlParam = openApi.depositApply;
            options.param = angular.copy($scope.param, {});
            options.param["formBidApply.formId"] = options.param["formBidApply.formId"]||options.param.formBidApply && options.param.formBidApply.formId || "";
            delete options.param.fundSoruceList
            delete options.param.receiveList
            delete options.param.formBidApply
            FormSubmit.submit(options,$scope);
        };
        $scope.showDepositApply=function(){
            var param={}
            param.type=1;
            $scope.searchparam=param
            $scope.searchtitle="款项来源人"
            $scope.apiurl=openApi.depositApplyHistory
            showSearchPicker()
        }
        $scope.showReceiptName=function(){
            var param={}
            $scope.searchparam=param
            $scope.searchtitle="收款账号"
            $scope.apiurl=openApi.depositApplyReceiptHistory
            showSearchPicker()
        }
        $scope.showReceivePerson=function(){
            var param={}
            param.type=2;            
            $scope.searchparam=param
            $scope.searchtitle="收款人"
            $scope.apiurl=openApi.depositApplyHistory
            showSearchPicker()
        }
        
        var showSearchPicker=function(){
            $scope.showSearchInput=true;
            $rootScope.$emit('showmasker');
        }
        $scope.handleSearch = function(data) {
          // 根据选择框赋值
          switch ($scope.searchtitle) {
            case "收款账号":
              $scope.param.receiptName = data[0];
              break;
            case "收款人":
              $scope.param.receivePerson = data[0];
              $scope.param.receiveAccount = data[1];
              $scope.param.receiveBank = data[2];
              break;
            case "款项来源人":
              $scope.param.fundSorucePerson = data[0];
              $scope.param.fundSoruceAccount = data[1];
              $scope.param.fundSoruceBank = data[2];
              break;
          }
          hideSearchPicker();
        };
        var hideSearchPicker = function () {
            $rootScope.$emit('closemasker');
            $scope.showSearchInput = false;
        };
        $scope.$on('closemasker', function () {
            hideSearchPicker()
        });
        $scope.validate = function() {
            if (!$scope.param.projectName) {
                Util.Toast("请填写工程名称");
                return;
            }
            if (!$scope.param.payType) {
                Util.Toast("请选择保证金类型");
                return;
            }
            if (!$scope.disabled() && !$scope.param.depositReturnId) {
                Util.Toast("请选择汇保证金时的编号");
                return;
            }
            if (!$scope.param.payWay) {
                Util.Toast("请选择付款方式");
                return;
            }
            if($scope.param.payType==1||$scope.param.payType==2){
              if (!$scope.param.fundSoruceMoney) {
                  if ($scope.param.fundSoruceMoney !== 0) {
                      Util.Toast("请填写来款金额");
                      return;
                  }
              }
            }
            if ($scope.param.payType == 3||$scope.param.payType == 4) {
                if (!$scope.param.receiveMoney) {
                    if ($scope.param.receiveMoney !== 0) {
                        Util.Toast("请填写收款金额");
                        return;
                    }
                }
            }
            if (!$scope.param.endDate) {
                Util.Toast("请选择到账截至日期");
                return;
            }
            if (!$scope.param.remark) {
                Util.Toast("请填写备注");
                return;
            }
            if (!$scope.validation.passvalidation) {
                Util.Toast($scope.validation.tip);
                return;
            }
            return true;
        }
        var init = function() {
            // 获取回显的详细信息
            Util.getDetailData(id, function(data) {
                $scope.param = data;
                // // 根据后台返回的字符串创建列表，后台没有返回的话就初始化一个列表
                $scope.param.fundSoruceList = $scope.param.fundSoruceDetail && JSON.parse($scope.param.fundSoruceDetail) || [];
                $scope.param.receiveList = $scope.param.receiveDetail && JSON.parse($scope.param.receiveDetail) || [];
                $scope.disableInput = !!($scope.param && $scope.param.disableInput)
                if(typeof $scope.param.lastReturnBack==="undefined"){
                    $scope.param.lastReturnBack=$scope.param.lastReturnBack||1
                }
            });
        };
        // 初始化
        init();

    });
